<template>
  <view class="container">
    <my-navbar-top></my-navbar-top>
    <view class="address" v-if="addressList.length > 0">
      <view
        class="address-item"
        v-for="item in addressList"
        :key="item.id"
        @click.stop="handleSelectAddress(item)"
      >
        <view class="address-item-info">
          <view class="address-item-info-name">{{ item.realname }}</view>
          <view class="address-item-info-phone">{{ item.telephone }}</view>
        </view>
        <view class="address-item-info-address">
          <view>
            {{ item.province }}{{ item.city }}{{ item.area }}{{ item.address }}
          </view>
          <!-- <view class="address-item-info-address-phone">
            {{ item.telephone }}
          </view> -->
        </view>

        <!-- 设置默认地址 和 编辑按钮 -->
        <view class="address-item-info-default">
          <view class="address-item-info-default-text">
            <text>设置默认地址</text>
            <switch
              :key="item.id"
              class="switch"
              :checked="!!item.is_default"
              @change="handleSwitchChange(item)"
              color="#C9A063"
              style="transform: scale(0.7); width: 100rpx"
            />
          </view>
          <view
            class="address-item-info-default-edit"
            @click.stop="handleAddAddress(item)"
            >编辑</view
          >
        </view>
      </view>
    </view>
    <view class="address-empty" v-else>
      <view class="address-empty-text">
        <view>您还没有地址</view>
        <view>请添加地址</view>
      </view>
    </view>
    <view class="address-add" @click="handleAddAddress()">
      <view class="address-add-text">添加新地址</view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'Address',
  data() {
    return {
      type: '', // select 选择地址  add 新增地址
      addressList: [],
    }
  },
  mounted() {
    // this.initData()
  },
  onLoad(e) {
    this.type = e.type || ''
  },
  onShow() {
    this.initData()
  },
  methods: {
    initData() {
      this.$api.getAddressList().then((res) => {
        console.log(res, 'res')
        this.addressList = res.data.user_address || []
      })
    },
    handleSwitchChange(item) {
      console.log(item)
      this.$api
        .updateAddressDefault({
          user_address_id: item.id,
        })
        .then((res) => {
          console.log(res)
          this.initData()
        })
    },
    // 新增和编辑地址
    handleAddAddress(item) {
      uni.navigateTo({
        url:
          '/pages/user/address/edit' +
          (item && item.id ? `?id=${item.id}` : ''),
      })
    },
    handleSelectAddress(item) {
      if (this.type == 'select') {
        uni.setStorageSync('currentAddress', item)
        uni.navigateBack()
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.address-empty {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  padding-top: 40%;
  &-text {
    font-size: 28rpx;
    color: var(--main-color);
    text-align: center;
  }
}
.address {
  width: 100%;
  padding: 50rpx 36rpx 154rpx 36rpx;
  overflow-y: auto;
  box-sizing: border-box;

  .switch {
    margin-left: 20rpx;
  }
  .address-item {
    width: 100%;
    background-color: #fff;
    box-sizing: border-box;
    margin-bottom: 28rpx;
    border-radius: 2rpx;
    padding: 30rpx;
    color: #545454;
    font-size: 24rpx;
    &-info {
      display: flex;
      align-items: center;
      margin-bottom: 24rpx;

      &-name {
        font-size: 28rpx;
        font-weight: bold;
        color: #545454;
      }
      &-phone {
        margin-left: 70rpx;
      }
      &-address {
        margin-bottom: 24rpx;
      }
      &-default {
        display: flex;
        align-items: center;
        justify-content: space-between;
        &-text {
          display: flex;
          align-items: center;
          justify-content: center;
        }
        &-edit {
          color: var(--main-color);
          font-size: 24rpx;
          border-bottom: 2rpx solid var(--main-color);
          line-height: 30rpx;
        }
      }
    }
  }
}
.address-add {
  width: 100%;
  background-color: #e7e7e7;
  border-radius: 2rpx;
  padding: 30rpx 33rpx;
  padding-bottom: calc(env(safe-area-inset-bottom) + 30rpx);
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  box-sizing: border-box;
  box-shadow: 0rpx -2rpx 10rpx 0rpx rgba(0, 0, 0, 0.1);
  &-text {
    height: 70rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 28rpx;
    color: #545454;
    font-weight: bold;
    background: var(--main-color);
    color: #fff;
    border-radius: 2rpx;
  }
}
</style>
